﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace ParticlePlayground
{
	// Token: 0x02000162 RID: 354
	[Serializable]
	public class ParticleProjectionC
	{
		// Token: 0x06000843 RID: 2115 RVA: 0x00030BE0 File Offset: 0x0002EDE0
		public virtual void Initialize()
		{
			if (!this.projectionTexture)
			{
				return;
			}
			this.Construct(this.projectionTexture, this.projectionTransform);
			if (!this.liveUpdate)
			{
				this.UpdateSource();
				this.Update();
			}
			this.initialized = true;
			this.hasRefreshed = false;
		}

		// Token: 0x06000844 RID: 2116 RVA: 0x00030C38 File Offset: 0x0002EE38
		public virtual void Construct(Texture2D image, Transform transform)
		{
			List<Color32> list = new List<Color32>();
			List<Vector3> list2 = new List<Vector3>();
			this.sourceColors = PlaygroundC.GetPixels(image);
			bool flag = false;
			if (PlaygroundC.reference)
			{
				flag = PlaygroundC.reference.buildZeroAlphaPixels;
			}
			int num = 0;
			int num2 = 0;
			for (int i = 0; i < this.sourceColors.Length; i++)
			{
				if (flag || this.sourceColors[i].a != 0)
				{
					list.Add(this.sourceColors[i]);
					list2.Add(new Vector3((float)num * this.projectionScale, (float)num2 * this.projectionScale, 0f));
				}
				num++;
				num %= image.width;
				if (num == 0 && i != 0)
				{
					num2++;
				}
			}
			this.sourceColors = list.ToArray();
			this.sourcePositions = list2.ToArray();
			this.targetPositions = new Vector3[this.sourcePositions.Length];
			this.targetNormals = new Vector3[this.sourcePositions.Length];
			this.targetParents = new Transform[this.sourcePositions.Length];
			this.hasProjected = new bool[this.sourcePositions.Length];
			this.positionLength = this.sourcePositions.Length;
			this.colorLength = this.sourceColors.Length;
			this.projectionTexture = image;
			this.projectionTransform = transform;
			this.projectionMatrix = default(Matrix4x4);
		}

		// Token: 0x06000845 RID: 2117 RVA: 0x00030DB8 File Offset: 0x0002EFB8
		public virtual void UpdateSource()
		{
			this.projectionPosition = this.projectionTransform.position;
			this.projectionDirection = this.projectionTransform.forward;
			this.projectionRotation = this.projectionTransform.rotation;
			this.projectionMatrix.SetTRS(this.projectionPosition, this.projectionRotation, this.projectionTransform.localScale);
		}

		// Token: 0x06000846 RID: 2118 RVA: 0x00030E1C File Offset: 0x0002F01C
		public virtual void Update()
		{
			for (int i = 0; i < this.positionLength; i++)
			{
				this.Update(i);
			}
		}

		// Token: 0x06000847 RID: 2119 RVA: 0x00030E48 File Offset: 0x0002F048
		public virtual void Update(int index)
		{
			index %= this.positionLength;
			Vector3 vector = this.projectionMatrix.MultiplyPoint3x4(this.sourcePositions[index] + new Vector3(this.projectionOrigin.x, this.projectionOrigin.y, 0f));
			if (this.collisionType == COLLISIONTYPEC.Physics3D)
			{
				RaycastHit raycastHit;
				if (Physics.Raycast(vector, this.projectionDirection, out raycastHit, this.projectionDistance, this.projectionMask))
				{
					this.targetPositions[index] = raycastHit.point + raycastHit.normal * this.surfaceOffset;
					this.targetNormals[index] = raycastHit.normal;
					this.targetParents[index] = raycastHit.transform;
					this.hasProjected[index] = true;
				}
				else
				{
					this.targetPositions[index] = PlaygroundC.initialTargetPosition;
					this.targetNormals[index] = Vector3.forward;
					this.hasProjected[index] = false;
					this.targetParents[index] = null;
				}
			}
			else
			{
				RaycastHit2D raycastHit2D = Physics2D.Raycast(vector, this.projectionDirection, this.projectionDistance, this.projectionMask, this.minDepth, this.maxDepth);
				if (raycastHit2D.collider != null)
				{
					this.targetPositions[index] = raycastHit2D.point + raycastHit2D.normal * this.surfaceOffset;
					this.targetNormals[index] = raycastHit2D.normal;
					this.targetParents[index] = raycastHit2D.transform;
					this.hasProjected[index] = true;
				}
				else
				{
					this.targetPositions[index] = PlaygroundC.initialTargetPosition;
					this.targetNormals[index] = Vector3.forward;
					this.hasProjected[index] = false;
					this.targetParents[index] = null;
				}
			}
		}

		// Token: 0x06000848 RID: 2120 RVA: 0x0003106C File Offset: 0x0002F26C
		public virtual Color32 GetColor(int index)
		{
			index %= this.colorLength;
			return this.sourceColors[index];
		}

		// Token: 0x06000849 RID: 2121 RVA: 0x0003108C File Offset: 0x0002F28C
		public virtual Vector3 GetPosition(int index)
		{
			index %= this.positionLength;
			return this.targetPositions[index];
		}

		// Token: 0x0600084A RID: 2122 RVA: 0x000310AC File Offset: 0x0002F2AC
		public virtual Vector3 GetNormal(int index)
		{
			index %= this.positionLength;
			return this.targetNormals[index];
		}

		// Token: 0x0600084B RID: 2123 RVA: 0x000310CC File Offset: 0x0002F2CC
		public virtual Transform GetParent(int index)
		{
			index %= this.positionLength;
			return this.targetParents[index];
		}

		// Token: 0x0600084C RID: 2124 RVA: 0x000310E0 File Offset: 0x0002F2E0
		public virtual bool HasProjection(int index)
		{
			index %= this.positionLength;
			return this.hasProjected[index];
		}

		// Token: 0x0600084D RID: 2125 RVA: 0x000310F4 File Offset: 0x0002F2F4
		public virtual ParticleProjectionC Clone()
		{
			ParticleProjectionC particleProjectionC = new ParticleProjectionC();
			if (this.sourceColors != null)
			{
				particleProjectionC.sourceColors = (Color32[])this.sourceColors.Clone();
			}
			if (this.sourcePositions != null)
			{
				particleProjectionC.sourcePositions = (Vector3[])this.sourcePositions.Clone();
			}
			if (this.targetPositions != null)
			{
				particleProjectionC.targetPositions = (Vector3[])this.targetPositions.Clone();
			}
			if (this.targetNormals != null)
			{
				particleProjectionC.targetNormals = (Vector3[])this.targetNormals.Clone();
			}
			if (this.hasProjected != null)
			{
				particleProjectionC.hasProjected = (bool[])this.hasProjected.Clone();
			}
			if (this.targetParents != null)
			{
				particleProjectionC.targetParents = (Transform[])this.targetParents.Clone();
			}
			particleProjectionC.projectionTexture = this.projectionTexture;
			particleProjectionC.projectionOrigin = this.projectionOrigin;
			particleProjectionC.projectionTransform = this.projectionTransform;
			particleProjectionC.projectionMatrix = this.projectionMatrix;
			particleProjectionC.projectionPosition = this.projectionPosition;
			particleProjectionC.projectionDirection = this.projectionDirection;
			particleProjectionC.projectionRotation = this.projectionRotation;
			particleProjectionC.projectionDistance = this.projectionDistance;
			particleProjectionC.projectionScale = this.projectionScale;
			particleProjectionC.projectionMask = this.projectionMask;
			particleProjectionC.collisionType = this.collisionType;
			particleProjectionC.minDepth = this.minDepth;
			particleProjectionC.maxDepth = this.maxDepth;
			particleProjectionC.surfaceOffset = this.surfaceOffset;
			particleProjectionC.liveUpdate = this.liveUpdate;
			particleProjectionC.hasRefreshed = this.hasRefreshed;
			particleProjectionC.initialized = this.initialized;
			particleProjectionC.colorLength = this.colorLength;
			particleProjectionC.positionLength = this.positionLength;
			return particleProjectionC;
		}

		// Token: 0x04000831 RID: 2097
		[HideInInspector]
		protected Color32[] sourceColors;

		// Token: 0x04000832 RID: 2098
		[HideInInspector]
		protected Vector3[] sourcePositions;

		// Token: 0x04000833 RID: 2099
		[HideInInspector]
		protected Vector3[] targetPositions;

		// Token: 0x04000834 RID: 2100
		[HideInInspector]
		protected Vector3[] targetNormals;

		// Token: 0x04000835 RID: 2101
		[HideInInspector]
		protected bool[] hasProjected;

		// Token: 0x04000836 RID: 2102
		[HideInInspector]
		protected Transform[] targetParents;

		// Token: 0x04000837 RID: 2103
		public Texture2D projectionTexture;

		// Token: 0x04000838 RID: 2104
		public Vector2 projectionOrigin;

		// Token: 0x04000839 RID: 2105
		public Transform projectionTransform;

		// Token: 0x0400083A RID: 2106
		public Matrix4x4 projectionMatrix;

		// Token: 0x0400083B RID: 2107
		public Vector3 projectionPosition;

		// Token: 0x0400083C RID: 2108
		public Vector3 projectionDirection;

		// Token: 0x0400083D RID: 2109
		public Quaternion projectionRotation;

		// Token: 0x0400083E RID: 2110
		public float projectionDistance = 1000f;

		// Token: 0x0400083F RID: 2111
		public float projectionScale = 0.1f;

		// Token: 0x04000840 RID: 2112
		public LayerMask projectionMask = -1;

		// Token: 0x04000841 RID: 2113
		public COLLISIONTYPEC collisionType;

		// Token: 0x04000842 RID: 2114
		public float minDepth = -1f;

		// Token: 0x04000843 RID: 2115
		public float maxDepth = 1f;

		// Token: 0x04000844 RID: 2116
		public float surfaceOffset;

		// Token: 0x04000845 RID: 2117
		public bool liveUpdate = true;

		// Token: 0x04000846 RID: 2118
		public bool hasRefreshed;

		// Token: 0x04000847 RID: 2119
		[HideInInspector]
		public bool initialized;

		// Token: 0x04000848 RID: 2120
		[HideInInspector]
		public int colorLength;

		// Token: 0x04000849 RID: 2121
		[HideInInspector]
		public int positionLength;
	}
}
